package com.biz.storage.adapter.impl;

import com.aliyuncs.utils.*;
import com.biz.storage.adapter.*;
import com.biz.storage.annotation.*;
import com.biz.storage.enums.*;
import com.biz.storage.util.*;
import lombok.*;
import org.springframework.web.multipart.*;

import javax.annotation.*;
import java.io.*;

/**
 * @Description
 *      AliyunOSS资源处理适配器
 * @Author liuxi
 * @Date 2024/4/26
 **/
@StorageAdapterType(EStorageType.ALIYUN_OSS)
public class AliyunOssStorageAdapter implements StorageAdapter {

    @Resource
    private AliyunOssUtil ossUtil;

    @Override
    public void createBucket(String bucket) {
        ossUtil.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        ossUtil.createBucket(bucket);
        String fileName = uploadFile.getName();
        if (!StringUtils.isEmpty(objectName)) {
            fileName = objectName + "/" + uploadFile.getName();
        }
        ossUtil.uploadFile(uploadFile.getInputStream(), bucket, fileName);
    }

    @Override
    public InputStream downLoad(String bucket, String objectName) {
        return ossUtil.downloadFile(objectName, bucket);
    }

    @Override
    public void deleteObject(String bucket, String objectName) {
        ossUtil.deleteObject(bucket, objectName);
    }
}
